package com.itheima.web.listener;

import com.itheima.autoconfig.green.AliyunGreenTemplate;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.mongo.MovementService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MovementListener {

    @DubboReference
    MovementService movementService;

    @Autowired
    AliyunGreenTemplate aliyunGreenTemplate;

    //监听指定的队列
    @RabbitListener(queuesToDeclare = @Queue("tanhua.movement.state"))
    public void checkMovement(String id){
        //1.通过动态id查询动态详情
        Movement movement = movementService.findById(new ObjectId(id));

        //2.获取动态的图片和文字进行审核
        Boolean textResult = aliyunGreenTemplate.checkText(movement.getTextContent());
        Boolean imageResult = aliyunGreenTemplate.checkImage(movement.getMedias());

        //3.若审核通过,将状态改成1
        if(textResult && imageResult){
            movement.setState(1);
        }else {
            //4.若审核通过,将状态改成2
            movement.setState(2);
        }

        //5.更新动态
        movementService.update(movement);
    }
}
